package com.fsy.task.selenium;

import com.fsy.task.domain.User;
import com.fsy.task.util.Constant;
import com.fsy.task.util.SchoolCodeUtil;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * selenium自动化支持程度不高  , 故不采用
 */
public class SeleniumUtil2 {

    /**
     * 获取访问 http://zync.njcedu.com/student/prese/teachplan/listdetail.htm?id=9670000063 页面所需的schoolToken
     * @param username
     * @param password
     * @return
     */
    public static String getSchoolToken(String username , String password , long taskId){
//        System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\MTS\\geckodriver.exe");
//        System.setProperty("webdriver.gecko.driver","/Users/fushiyong/Downloads/geckodriver");
        //F:\Mozilla Firefox\geckodriver.exe
        Properties configProperties = new Properties();
        try {
            configProperties.load(new FileReader(new File("zgtda/config.properties")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.setProperty("webdriver.gecko.driver",configProperties.getProperty("geckodriver"));

        // TODO Auto-generated method stub
        //如果测试的浏览器没有安装在默认目录，那么必须在程序中设置
        //bug1:System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
        //bug2:System.setProperty("webdriver.chrome.driver", "C://Users//Yoga//Downloads//chromedriver_win32//chromedriver.exe");
        //System.setProperty("webdriver.chrome.driver", "D://tanzhenTest//chromedriver_win32//chromedriver.exe");
        FirefoxDriver firefoxDriver = new FirefoxDriver();

        firefoxDriver.get("http://passport.njcedu.com/#/login");

        WebDriverWait webDriverWait=new WebDriverWait(firefoxDriver,5);

        WebElement agreeButton = firefoxDriver.findElement(By.className("agree_btn"));

        agreeButton.click();


        WebElement e= firefoxDriver.findElement(By.cssSelector("input[type=password]"));

        e.clear();

        e.sendKeys(password);

        webDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/input[1]"))).sendKeys(username);

        WebElement e1= firefoxDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div[5]"));

        e1.click();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e2) {
            e2.printStackTrace();
        }
//        String userSchoolId = firefoxDriver.getPageSource();


//        String nickName = firefoxDriver.findElementByXPath("/html/body/div[1]/div/div[1]/div[1]/div/div[2]/div/span").getText();


        String  schoolCode = SchoolCodeUtil.getSchoolCodeFromUsername(username);
        String schoolTokenUrl = "http://"+schoolCode+".njcedu.com/student/prese/teachplan/listdetail.htm?id=" + taskId;
        firefoxDriver.get(schoolTokenUrl);

        String schoolToken = null;
        do{

            try {
                firefoxDriver.manage().window();
//                WebElement agreeButton = firefoxDriver.findElement(By.className("agree_btn"));
                schoolToken = firefoxDriver.manage().getCookieNamed("schoolToken").getValue();
                Thread.sleep(Constant.TIMEOUT * 1000);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }catch (UnhandledAlertException alertExc){
                System.out.println("skip mail verify . ");
            }
            catch (Exception e3){
                e3.printStackTrace();
            }
        }while(schoolToken == null);

        firefoxDriver.quit();

        return schoolToken;
    }
}
